import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { User } from './user';

/*
  Generated class for the LoginDataProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class LoginDataProvider {
  private readonly baseUrl:string ='https://api2.bmob.cn/1/';
  private readonly httpHeaders:HttpHeaders=new HttpHeaders({
    'X-Bmob-Application-Id':'f68b5d0362c7c0522693b9685d255d9a',
    'X-Bmob-REST-API-Key':'0d3668e8eaeacc9de420e64b4f5415d7',
    'Content-Type':'application/json'
  });
  constructor(public http: HttpClient,) {
   
  }
/**
 * 登录
 */

  login(username:string,password:string):Observable<User>{
    let _params=new HttpParams().set('username',username).set('password',password);
    return this.http.get<User>(`${this.baseUrl}login`,{headers:this.httpHeaders,params:_params});
  }
/**
 * 注册
 * @param username 
 * @param password 
 */
 register(username:string,password:string):Observable<User>{
    let registerUser={username:username,password:password};
    return this.http.post<User>(`${this.baseUrl}users`,registerUser,{headers:this.httpHeaders});
  }
}
